rm(list = ls())
setwd("~/Desktop/QJPS Replication")
data <- read.csv("all_countries_ideal.csv")

data$match <- paste(data$cown, data$year, sep = "-")

#length
data2 <- read.dta13("data/ccpcnc_v2_small.dta")
#get rid of state-years where data aren't collected completely
data2 <- data2[!is.na(data2$preamble),]
data2$match <- paste(data2$cowcode, data2$year, sep = "-")
m <- match(data$match, data2$match)
table(is.na(m))
data$length <- data2$length[m]

#number of features
data2 <- read.csv("data/roll_call_matrix.csv")
data2 <- data2[!is.na(data2$preamble),]
data2$match <- paste(data2$cowcode, data2$year, sep = "-")
rownames(data2) <- data2$match
data2$X <- NULL
data2$country <- NULL
data2$year <- NULL
data2$syst <- NULL
data2$systid <- NULL
data2$systyear <- NULL
data2$evnt <- NULL
data2$evntid <- NULL
data2$evntyear <- NULL
data2$evnttype <- NULL

#count number of non-missing values (minus two for cowcode and match)
features <- apply(data2, 1, function(x) sum(!is.na(x), na.rm = T)) - 2

m <- match(data$match, names(features))
table(is.na(m))
data$features <- features[m]

# enumeration of positive rights
rights <- c("citrev_4", "citren", "rulelaw", "equal", "equalgr_1", "equalgr_2", "equalgr_3", 
            "equalgr_4", "equalgr_5", "equalgr_6", "equalgr_7", "equalgr_8", "equalgr_9", 
            "equalgr_10", "equalgr_11", "equalgr_12", "equalgr_13", "equalgr_14", "equalgr_15", 
            "equalgr_16", "binding", "infoacc", "infoaccw1", "infoaccw2", "infoaccw3", "libel", 
            "freerel", "seprel", "reltax", "exprop", "socecon", "remuner",
            "jointrde1", "strike", "leisure", "standliv", "transfer", "testate", "inherit",
            "busines", "conright", "proprght1", "freecomp", "scifree", "occupate", "safework1", 
            "safework3", "childwrk1", "shelter", "marriage1", "marriage2", "samesexm", "matequal", 
            "childpro", "civmar", "selfdet", "healthr", "healthf", "life", "slave1", "torture1", 
            "cruelty", "privacy", "freemove", "opinion", "express", "petition", "censor1", "press1",
            "assem", "assoc", "inalrght", "devlpers", "nomil1", "asylum", "arms", "opgroup", "langprot1", 
            "edfree", "acfree", "achighed1")

data2$exprop <- ifelse(data2$exprop == 0, 1, 0)

rights.provisions <- apply(data2[,colnames(data2) %in% c(rights)], 1, function(x) sum(x, na.rm = T))

m <- match(data$match, names(rights.provisions))
table(is.na(m))
data$rights <- rights.provisions[m]


#Figure A2 - First panel
par(mar = c(4, 5, 3, 2))
hist(data$length, xlab = "Number of Words (in thousands)", axes = F, 
     ylim = c(0, 1000), xlim = c(0, 275000), 
     ylab = "Frequency", main = "Constitution Length", col = "grey")
axis(1, at = seq(0, 275000, 50000), labels = seq(0, 275, 50))
axis(2, at = seq(0, 1000, 250), las = 2)

#Figure A2 - Second panel
hist(data$features, xlab = "Number of Features", axes = F, 
     ylim = c(0, 300), xlim = c(0, 1200), 
     ylab = "Frequency", main = "Constitution Features", col = "grey")
axis(1, at = seq(0, 1200, 200), labels = seq(0, 1200, 200))
axis(2, at = seq(0, 300, 50), las = 2)

#Figure A2 - Third panel
hist(data$rights, xlab = "Enumerated Rights", axes = F, 
     ylim = c(0, 300), xlim = c(0, 75), 
     ylab = "Frequency", main = "Number of Rights", col = "grey")
axis(1, at = seq(0, 75, 15), labels = seq(0, 75, 15))
axis(2, at = seq(0, 300, 50), las = 2)
